﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace RaceHFTools.Utils
{
    class Net
    {
        public static async Task<byte[]> Download(string url)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                using (System.IO.Stream netstream = response.GetResponseStream())
                {
                    List<byte> data = new List<byte>();
                    byte[] readbuff = new byte[1024];
                    int byteread = 0;
                    do
                    {
                        byteread = await netstream.ReadAsync(readbuff, 0, readbuff.Length);
                        byte[] buff = new byte[byteread];
                        Array.Copy(readbuff, 0, buff, 0, byteread);
                        data.AddRange(buff);
                    } while (byteread > 0);
                    return data.ToArray();
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
